<?php

namespace App\Http\Controllers\Api;

use App\Models\AuctionLog;
use App\Models\Goods;
use App\Models\Message;
use App\Models\Order;
use App\Models\PhoneCode;
use App\Models\User;

use App\Models\UserBuyer;
use App\Models\UserSeller;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;
use APP\Tools\Init;

class UserController extends Controller
{


    public function hQian()
    {
        //初始配置
        //公用配置
        $openId = env('OPEN_ID');
        $appSecret = env('APP_SECRET');
        //测试环境
        $baseUrl = env('BASE_URL');

        //随机生成nonce
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr($chars, 0, 8) . '-';
        $uuid .= substr($chars, 8, 4) . '-';
        $uuid .= substr($chars, 12, 4) . '-';
        $uuid .= substr($chars, 16, 4) . '-';
        $uuid .= substr($chars, 20, 12);
        $this->uUid = $uuid;
        $url = $baseUrl . '/contract/from/tpl';

        $projectSn = 'PM' . mt_rand(uniqid());
        //参与加密
        $posts = [
            'openId' => $openId,
            'version' => '1.0',
            'nonce' => $uuid,
            'projectSn' => $projectSn,
            'files' => [
                ['sn' => 'sn15204922931'], //如多文件 多sn都需要参与加密
//        ['sn' => 'sn1520492210'],
            ]
        ];
        ksort($posts);

        $posts['sign'] = sha1(http_build_query($posts) . "&nonce=" . $posts['nonce'] . "&appSecret=" . $appSecret);
        /* var_dump($posts['nonce']);
        var_dump($posts['sign']);
        exit; */
//非加密部分
        /* print_r($posts);
        exit; */
        $posts['projectName'] = '拍卖合同' . date('mdHis');
        $posts['notifyUrl'] = 'https://www.notify_update.com';
//第1个合同


        $posts['files'][0]['name'] = '合同名称';
        $posts['files'][0]['templateId'] = 'HQ75975829';//须在和签平台首先创建模板，得到此模板ID
        $posts['files'][0]['permission'] = '*';
        $posts['files'][0]['params'] = json_encode(array('j' => '张四', 'y' => '张四'));//如无可写空json  '{}'

        $posts['files'][0]['sign'][0]['idCardType'] = '2';
        $posts['files'][0]['sign'][0]['idCardNo'] = '500233199204174111';
        $posts['files'][0]['sign'][0]['mobile'] = '18203007008';
        $posts['files'][0]['sign'][0]['name'] = '卢小艳';
        $posts['files'][0]['sign'][0]['identity'] = '1';
        $posts['files'][0]['sign'][0]['auto'] = 1;
        $posts['files'][0]['sign'][0]['reason'] = 'sn' . time();
        $posts['files'][0]['sign'][0]['location'] = 'sn' . time();


        $posts['files'][0]['sign'][1]['idCardType'] = '2';
        $posts['files'][0]['sign'][1]['idCardNo'] = '500233199201093111';
        $posts['files'][0]['sign'][1]['mobile'] = '18203007009';
        $posts['files'][0]['sign'][1]['name'] = '皮华桥';
        $posts['files'][0]['sign'][1]['identity'] = '2';
        $posts['files'][0]['sign'][1]['auto'] = 1;
        $posts['files'][0]['sign'][1]['reason'] = 'sn' . time();
        $posts['files'][0]['sign'][1]['location'] = 'sn' . time();


        $posts['files'][0]['sign'][1]['openId'] = 'b6e768656923edd23ff3b70e052f9aea12fedd23'; //openId 和 auth 只需要传一个
        /* $posts['files'][0]['sign'][1]['auto'] = 1;
        $posts['files'][0]['sign'][1]['identity'] = '2'; */
        $result = $this->exec_post($url, $posts);
        //$posts['files'][0]['sign'][1]['reason'] = 'sn'.time();//非必填
        //$posts['files'][0]['sign'][1]['stamp'] = 'invisible';//非必填
        //$posts['files'][0]['sign'][1]['location'] = 'sn'.time();//非必填


//第2个合同 如多文件 将以上file 0字段重复
//$posts['files'][1]['name'] …………

    }

    //公用方法
    function exec_post($url, $posts)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($posts));

        $result = curl_exec($ch);
        if (!$result) {
            var_dump(curl_error($ch));
        }
        curl_close($ch);
        return json_decode($result, true);
    }

    /**
     * 编辑昵称资料
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function nameSave(Request $request)
    {
        $params = $request->only(['name', 'head']);
        $validator = \Validator::make($params, [
            'name' => 'required|string|max:30',

        ], [
            'name.*' => '昵称填写错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        try {
            $user = \Auth::user();
            $user->name = $request->name;
            if (!$user->save()) {
                throw  new \Exception('修改失败');
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

    }

    /**
     * 编辑头像资料
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function headSave(Request $request)
    {
        $params = $request->only(['head']);
        $validator = \Validator::make($params, [
            'head' => 'required',
        ], [
            'head.*' => '头像不能为空',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        try {
            $user = \Auth::user();
            $user->head = $request->head;
            if (!$user->save()) {
                throw  new \Exception('修改失败');
            }
            return $this->success(['msg' => '修改成功']);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

    }

    /**
     * 用户信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function userInfo()
    {
        $user = \Auth::user();

        return $this->success($user);
    }

    /**
     * 图片上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadImg(Request $request)
    {

        //上传文件最大大小,单位M
        $maxSize = 10;
        //支持的上传图片类型
//        $allowed_extensions = ["png", "jpg", "gif"];
        //返回信息json
        $data = ['code' => 200, 'msg' => '上传失败', 'data' => ''];
        $file = $request->file('file');

        //检查文件是否上传完成
        if ($file->isValid()) {
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
//            if (!in_array(strtolower($ext), $allowed_extensions)) {
//                $data['msg'] = "请上传" . implode(",", $allowed_extensions) . "格式的图片";
//                return $this->error($data['msg']);
//            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                $data['msg'] = "文件大小限制" . $maxSize . "M";
                return $this->error($data['msg']);
            }
        } else {
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }

        $filedir = "upload/" . date('Y-m-d', time()); //2、定义图片上传路径
        $filename = md5(uniqid('image', true) . time()) . '.' . $file->getClientOriginalExtension();

        $file->move($filedir, $filename);

        $url = '/' . $filedir . '/' . $filename;

        $data = [
            'msg' => '上传成功',
            'data' => $filename,
            'url' => $url
        ];


        return $this->success($data);
    }

    /**
     * 图片上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadfile(Request $request)
    {

        //上传文件最大大小,单位M
        $maxSize = 10;
        //支持的上传图片类型
        $allowed_extensions = ["png", "jpg", "gif"];
        //返回信息json
        $data = ['code' => 200, 'msg' => '上传失败', 'data' => ''];
        $file = $request->file('file');

        //检查文件是否上传完成
        if ($file->isValid()) {
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                $data['msg'] = "请上传" . implode(",", $allowed_extensions) . "格式的图片";
                return $this->error($data['msg']);
            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                $data['msg'] = "图片大小限制" . $maxSize . "M";
                return $this->error($data['msg']);
            }
        } else {
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }

        $filedir = "upload/" . date('Y-m-d', time()); //2、定义图片上传路径
        $filename = md5(uniqid('image', true) . time()) . '.' . $file->getClientOriginalExtension();

        $file->move($filedir, $filename);

        $url = '/' . $filedir . '/' . $filename;

        $data = [
            'msg' => '上传成功',
            'data' => $filename,
            'url' => $url
        ];


        return $this->success($data);
    }

    /**
     * 发布产品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function releaseGoods(Request $request)
    {
        $user = \Auth::user();
        if($user->is_seller != 1){
            return $this->error('请先认证卖家');
        }

        $params = $request->only(['title', 'introduce', 'start_price', 'scope_price', 'reserve_price', 'priorityer', 'pay_type', 'price','image','file_s']);
        $validator = \Validator::make($params, [
            'title' => 'required|string|max:50',
            'introduce' => 'required|string|max:300',
            'start_price' => 'required',
            'scope_price' => 'required',
            'price' => 'required',
            'pay_type' => 'required',

        ], [
            'title.required' => '名称不能为空',
            'title.max' => '名称长度最大为100个字符串',
            'introduce.required' => '描述不能为空',
            'introduce.max' => '描述长度最大为300个字符串',
            'start_price.required' => '起拍价不能为空',
            'scope_price.required' => '加价幅度不能为空',
            'price.required' => '保证金不能为空',
            'pay_type.required' => '支付方式不能为空',

        ]);
//        dd($params);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $data = [
            'user_id' => \Auth::id(),
            'title' => $request->title, //名称
            'introduce' => $request->introduce, //介绍
            'price' => $request->price, //保证金
            'start_price' => $request->start_price,//起拍价
            'scope_price' => $request->scope_price,//加价幅度
            'reserve_price' => $request->reserve_price ?? 0,//保留价
            'priorityer' => $request->priorityer ?? 0,//优先购买人
            'pay_type' => $request->pay_type,//支付方式
            'status' => GOODS_STATUS_STAY_AUDIT,//待审核
        ];
//        dd($data);
//        dd($data);
        //产品图片
        $images = [];
        $goodsImg = $request->image;
        if (!empty($goodsImg)) {
            if (count($goodsImg) > 0) {
                foreach ($goodsImg as $item) {
                    $images[] = [
                        'url' => $item,
                        'type' => 1,
                        'created_at'=>date('Y-m-d H:i:s')
                    ];
                }
            }
        }
        //相关文件
        $files = [];
        $goodsFiles = $request->file_s;
//        dd($goodsFiles);
        if (!empty($goodsFiles)) {
            if (count($goodsFiles) > 0) {
                foreach ($goodsFiles as $item) {
                    $files[] = [
                        'url' => $item,
                        'type' => 2,
                        'created_at'=>date('Y-m-d H:i:s')
                    ];
                }
            }
        }
//        dd($files);
        //添加数据
        try {
            $re = Goods::addGoods($data, $images, $files);
//            dd($re);
            if ($re) {
                return $this->success(['msg' => '发布成功']);

            }
            throw  new  \Exception('发布失败');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

    }


    /**
     * 重新编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveGoods(Request $request)
    {
        $params = $request->only(['goods_id','title', 'introduce', 'start_price', 'scope_price', 'reserve_price', 'priorityer', 'pay_type', 'price','image','file_s']);
        $validator = \Validator::make($params, [
            'goods_id' => 'required|exists:goods,id',
            'title' => 'required|string|max:50',
            'introduce' => 'required|string|max:300',
            'start_price' => 'required',
            'scope_price' => 'required',
            'price' => 'required',
            'pay_type' => 'required',

        ], [
            'goods_id.*' => '参数错误',
            'title.required' => '名称不能为空',
            'title.max' => '名称长度最大为100个字符串',
            'introduce.required' => '描述不能为空',
            'introduce.max' => '描述长度最大为300个字符串',
            'start_price.required' => '起拍价不能为空',
            'scope_price.required' => '加价幅度不能为空',
            'price.required' => '保证金不能为空',
            'pay_type.required' => '支付方式不能为空',

        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $id = $request->goods_id;
        $data = [
            'title' => $request->title, //名称
            'introduce' => $request->introduce, //介绍
            'price' => $request->price, //保证金
            'start_price' => $request->start_price,//起拍价
            'scope_price' => $request->scope_price,//加价幅度
            'reserve_price' => $request->reserve_price ?? 0,//保留价
            'priorityer' => $request->priorityer ?? 0,//优先购买人
            'pay_type' => $request->pay_type,//支付方式
            'status' => GOODS_STATUS_STAY_AUDIT,//待审核
        ];
        $images = [];
        $goodsImg = $request->image;
        if (!empty($goodsImg)) {
            if (count($goodsImg) > 0) {
                foreach ($goodsImg as $item) {
                    $images[] = [
                        'url' => $item,
                        'type' => 1,
                        'created_at'=>date('Y-m-d H:i:s')
                    ];
                }
            }
        }
        //相关文件
        $files = [];
        $goodsFiles = $request->file_s;
//        dd($goodsFiles);
        if (!empty($goodsFiles)) {
            if (count($goodsFiles) > 0) {
                foreach ($goodsFiles as $item) {
                    $files[] = [
                        'url' => $item,
                        'type' => 2,
                        'created_at'=>date('Y-m-d H:i:s')
                    ];
                }
            }
        }

        $re = Goods::edit($id, $data,$files,$images);
        if ($re) {
            return $this->success(['msg' => '修改成功']);
        }
        return $this->error('修改失败');
    }


    /**
     * 我是卖家
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myReleaseGoods(Request $request)
    {
        $params = $request->only(['status', 'auctionStatus']);
        $validator = \Validator::make($params, [
            'status' => [Rule::in([1, 2, 3, 4])],
//            'auction_status' => [Rule::in([1, 2, 3])],
        ], [
            'status.in' => '状态值错误',
//            'auction_status.in' => '状态值错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $status = $request->status;
//        $auctionStatus = $request->auction_status;
        $user_id = \Auth::id();
        $data = Goods::getMyReleaseGoodsAll($user_id, $status);

        return $this->success($data);
    }

    /**
     * 我是买家
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myBiddingGoods(Request $request)
    {
        $params = $request->only(['auctionStatus']);
        $validator = \Validator::make($params, [
            'auctionStatus' => [Rule::in([2, 3])],
        ], [
            'auctionStatus.in' => '状态值错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        //查询我竞拍过的商品
        $user_id = \Auth::id();
//        $re = Order::myOrder($user_id);
//        $data = [];
//        if (count($re) == 0) {
//            return $this->success($data);
//        }

//        $goods_ids = array_unique($re);
       $goods_ids =  AuctionLog::where('user_id',$user_id)->pluck('goods_id')->toArray();
//        dd($goods_ids);
        $auctionStatus = $request->auctionStatus;
        $data = Goods::getMyAuctionGoodsAll(array_unique($goods_ids), $auctionStatus);

        return $this->success($data);
    }


    /**
     *参加拍卖
     * @param Request $request
     */
    public function biddingLog(Request $request)
    {
        $user = \Auth::user();
        if($user->is_buyers != 1){
            return $this->error('请先认证买家');
        }

        $user_id = \Auth::id();
        $goods_id = $request->goods_id;
        $price = $request->price;

        $params = $request->only(['goods_id','price']);
        $validator = \Validator::make($params, [
            'goods_id' => 'required|exists:goods,id',
            'price' => 'required',
        ], [
            'goods_id.*' => '产品参数错误',
            'price.*' => '金额不能为空',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        try {
            \DB::beginTransaction();
            //查询当前最高价
            $auticon = AuctionLog::where('goods_id', $goods_id)->select('id', 'user_id', 'goods_id', 'price')->orderBy('price', 'desc')->first();
            if ($auticon) {
                //获取当前商品的规则
                $goods = Goods::find($goods_id);
                //获取加价幅度
                //当前最低价
//            dd($auticon->toArray());
                if ($auticon->user_id == $user_id && $auticon->price  == $price ) {
                    throw  new  \Exception('已是当前最高价');
                }

                $lowest_price = bcadd($auticon->price, $goods->scope_price, 2);
                if ($lowest_price > $price || $auticon->price >= $price) {

                    throw  new  \Exception('出价不符合规则,最低出价为' . $lowest_price);
                }
            }
            //查询加价幅度
            AuctionLog::create([
                'user_id' => $user_id,
                'goods_id' => $goods_id,
                'price' => $price
            ]);

            \DB::commit();
            return $this->success(['msg' => '添加成功']);
        } catch (\Exception $exception) {

            \DB::rollBack();
            return $this->error($exception->getMessage());
        }


    }


    /**
     * 卖家认证
     */
    public function userSeller(Request $request)
    {
        $params = $request->only(['company', 'mobile', 'license', 'phone_code', 'code']);
        $validator = \Validator::make($params, [
            'mobile' => 'required|regex:/^1[345789]\d{9}$/|size:11',
            'company' => 'required|string|max:30',
            'license' => 'required',
            'phone_code' => 'required',
            'code' => 'required',

        ], [
            'mobile.required' => '手机号码错误',
            'mobile.regex' => '手机号码错误',
            'license.*' => '营业执照必填',
            'company.required' => '公司名称必填',
            'company.max' => '公司名称太长了',
            'phone_code.required' => '手机验证码必填',
            'code.required' => '信用统一代码'
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $data = [
            'user_id' => \Auth::id(),
            'company' => $request->company,
            'mobile' => $request->mobile,
            'license' => $request->license,
            'code' => $request->code,
        ];
        $re = PhoneCode::validation($request->mobile, $request->phone_code, 3);
        if (!$re) {
            return $this->error('验证码错误');
        }

        $re = UserSeller::certification($data);

        if ($re) {
            return $this->success(['msg' => '提交成功']);
        }
        return $this->error('提交失败');
    }


    /**
     * 买家认证
     */
    public function userBuyer(Request $request)
    {
        $params = $request->only(['name', 'mobile', 'id_card', 'id_card_z', 'id_card_f', 'phone_code']);
        $validator = \Validator::make($params, [
            'mobile' => 'required|regex:/^1[345789]\d{9}$/|size:11',
            'name' => 'required|string|max:30',
            'id_card' => 'required|string|size:18',
            'id_card_z' => 'required',
            'id_card_f' => 'required',
            'phone_code' => 'required',


        ], [
            'mobile.required' => '手机号码错误',
            'mobile.regex' => '手机号码错误',
            'mobile.*' => '手机号码错误',
            'name.required' => '名字必填',
            'id_card.required' => '名字太长了',
            'id_card.size' => '身份证位数不对',
            'id_card_z.size' => '请上传身份证正面照',
            'id_card_f.size' => '请上传身份证正面照',
            'phone_code.required' => '手机验证码必填',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $re = PhoneCode::validation($request->mobile, $request->phone_code, 3);
        if (!$re) {
            return $this->error('验证码错误');
        }
        $data = [
            'user_id' => \Auth::id(),
            'mobile' => $request->mobile,
            'name' => $request->name,
            'id_card' => $request->id_card,
            'id_card_z' => $request->id_card_z,
            'id_card_f' => $request->id_card_f,
        ];
        $re = UserBuyer::certification($data);

        if ($re) {
            return $this->success(['msg' => '提交成功']);
        }
        return $this->error('提交失败');
    }


    /**
     * 买家信息
     * @param Request $request
     */
    public function userBuyerDetail(Request $request)
    {

        $data = UserBuyer::where('user_id', \Auth::id())->first();
        return $this->success($data);
    }


    /**
     * 修改买家资料
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveBuyer(Request $request)
    {
        $params = $request->only(['name', 'mobile', 'id_card', 'id_card_z', 'id_card_f', 'phone_code']);
        $validator = \Validator::make($params, [
            'mobile' => 'required|regex:/^1[345789]\d{9}$/|size:11',
            'name' => 'required|string|max:30',
            'id_card' => 'required|string|size:18',
            'id_card_z' => 'required',
            'id_card_f' => 'required',
            'phone_code' => 'required',
        ], [
            'mobile.required' => '手机号码错误',
            'mobile.regex' => '手机号码错误',
            'mobile.*' => '手机号码错误',
            'name.required' => '名字必填',
            'id_card.required' => '名字太长了',
            'id_card.size' => '身份证位数不对',
            'id_card_z.size' => '请上传身份证正面照',
            'id_card_f.size' => '请上传身份证正面照',
            'phone_code.required' => '手机验证码必填',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $re = PhoneCode::validation($request->mobile, $request->phone_code, 3);
        if (!$re) {
            return $this->error('验证码错误');
        }

        try {
            $user = UserBuyer::where('user_id', \Auth::id())->first();
            if ($user->status == 2) {
                return $this->error('审核已通过');
            }
            $user->name = $request->name;
            $user->mobile = $request->mobile;
            $user->id_card = $request->id_card;
            $user->id_card_z = $request->id_card_z;
            $user->id_card_f = $request->id_card_f;
            $user->status = 1;
            if ($user->save()) {
                return $this->success(['msg' => '修改成功']);
            }
        }catch (\Exception $exception){
            info('买家'.$exception->getMessage());
            return $this->error($exception->getMessage());
        }



    }

    /**
     * 修改卖家
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveSeller(Request $request)
    {
        $params = $request->only(['company', 'mobile', 'license', 'phone_code', 'code']);
        $validator = \Validator::make($params, [
            'mobile' => 'required|regex:/^1[345789]\d{9}$/|size:11',
            'company' => 'required|string|max:30',
            'license' => 'required',
            'code' => 'required',
            'phone_code' => 'required',
        ], [
            'mobile.required' => '手机号码错误',
            'mobile.regex' => '手机号码错误',
            'mobile.*' => '手机号码错误',
            'company.required' => '公司名称必填',
            'company.max' => '公司名称太长了',
            'code.required' => '信用统一代码',
            'phone_code.required' => '手机验证码必填',
            'license.required' => '上传营业执照',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $re = PhoneCode::validation($request->mobile, $request->phone_code, 3);
        if (!$re) {
            return $this->error('验证码错误');
        }
        try {
            $user = UserSeller::where('user_id', \Auth::id())->first();
            if ($user->status == 2) {
                return $this->error('审核已通过');
            }
            $user->mobile = $request->mobile;
            $user->company = $request->company;
            $user->license = $request->license;
            $user->code = $request->code;
            $user->status = 1;

            if ($user->save()) {
                return $this->success(['msg' => '修改成功']);
            }
        }catch (\Exception $exception){
            info('卖家'.$exception->getMessage());
            return $this->error($exception->getMessage());
        }

    }

    /**
     * 卖家信息
     * @param Request $request
     */
    public function userSellerDetail(Request $request)
    {
        $data = UserSeller::where('user_id', \Auth::id())->first();
        return $this->success($data);
    }

    /**
     * 修改手机号一
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function savePhoneFirst(Request $request)
    {
        $user = \Auth::user();
        $re = PhoneCode::validation($user->phone, $request->phone_code, 2);


        if (!$re) {
            return $this->error('验证码错误');
        }
        $user->save_status = 1;
        $user->save();
        return $this->success(['msg' => '验证成功']);
    }

    /**
     * 修改手机号码二
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function savePhoneTwo(Request $request)
    {

        $users = User::where('phone', $request->phone)->first();
        if ($users) {
            return $this->error('手机号码以存在');
        }
        $re = PhoneCode::validation($request->phone, $request->phone_code, 2);
        if (!$re) {
            return $this->error('验证码错误');
        }
        $user = \Auth::user();
        if ($user->save_status != 1) {

            return $this->error('错误操作');
        }
        $user->phone = $request->phone;
        $user->save_status = 0;
        if ($user->save()) {
            return $this->success(['msg' => '修改成功']);
        }

    }

}
